/**
 * 
 */
package com.swim.swimteach.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.swim.swimteach.entity.SwimTeach;
import com.swim.swimteach.dao.SwimTeachDao;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 游泳教学Service
 * @author 刘军武
 * @version 2021-05-17
 */
@Service
@Transactional(readOnly=true)
public class SwimTeachService extends CrudService<SwimTeachDao, SwimTeach> {
	
	@Value("${server.context-path}")
	private String contextPath;
	
	/**
	 * 获取单条数据
	 * @param swimTeach
	 * @return
	 */
	@Override
	public SwimTeach get(SwimTeach swimTeach) {
		return super.get(swimTeach);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param swimTeach
	 * @return
	 */
	@Override
	public Page<SwimTeach> findPage(Page<SwimTeach> page, SwimTeach swimTeach) {
		Page<SwimTeach> pageList = super.findPage(page, swimTeach);
		List<SwimTeach> swimTeachList = pageList.getList();
		if (!CollectionUtils.isEmpty(swimTeachList)) {
			for (SwimTeach swimTeachOne : swimTeachList) {
				//获取游泳教学封面图片
				List<FileUpload> fileUploadListOne = FileUploadUtils.findFileUpload(swimTeachOne.getId(), "swimTeach_image");
				if (!CollectionUtils.isEmpty(fileUploadListOne)) {
					swimTeachOne.setSwimTeachImageUrl(contextPath + "/userfiles/fileupload/" + fileUploadListOne.get(0).getFileEntity().getFilePath() + fileUploadListOne.get(0).getFileEntity().getFileId() + "." + fileUploadListOne.get(0).getFileEntity().getFileExtension());
				}
			}
		}
		pageList.setList(swimTeachList);
		return pageList;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param swimTeach
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(SwimTeach swimTeach) {
		super.save(swimTeach);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(swimTeach.getId(), "swimTeach_image");
	}
	
	/**
	 * 更新状态
	 * @param swimTeach
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(SwimTeach swimTeach) {
		super.updateStatus(swimTeach);
	}
	
	/**
	 * 删除数据
	 * @param swimTeach
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(SwimTeach swimTeach) {
		super.delete(swimTeach);
	}
	
}